package net.liteheaven.xblib.plugin;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.text.TextUtils;

import net.liteheaven.hidefix.ReflectionHelper;

import java.lang.reflect.Method;

/**
 * 插件类
 *
 * Created by LiangYu on 16/8/29
 */
public class XPlugin {

    private String pluginName;

    private AssetManager mAssetManager; // 托管插件的资源
    private Resources.Theme mTheme; // 托管插件的样式
    private Resources mResources; // 托管插件的资源

    private ClassLoader classLoader;

    // disabled
    private XPlugin(){
    }

    protected XPlugin(Builder builder){
        pluginName = builder.pluginName;
    }

    private AssetManager createAssetManager(){
        AssetManager assetManager = null;
        try {
            assetManager = AssetManager.class.newInstance();
            if (!addAssetPath(assetManager)){
                XPluginLog.i("addAssetPath failed");
            }
        } catch (Exception e){
            XPluginLog.i(e);
        }
        return assetManager;
    }

    private ClassLoader createClassLoader(){
        String inner = PathUtil.getInnerApkPath(pluginName);
        return PathUtil.getClassLoader(inner);
    }

    public void attachActivity(XProxyActivity activity) throws XPluginException{
        AssetManager assetManager = getAssetManager();
        if (assetManager == null){
            throw new XPluginException("assetManager is null.");
        }

        // 活动连接成功后更改资源
        Resources lastResources = activity.getResources();
        Resources.Theme lastTheme = activity.getTheme();

        Resources.Theme newTheme = null;
        Resources newResources = new Resources(assetManager,
                lastResources.getDisplayMetrics(),
                lastResources.getConfiguration());

        if (newResources != null){
            newTheme = newResources.newTheme();
            newTheme.setTo(lastTheme);
        }

//        int [] themeId = android.support.v7.appcompat.R.styleable.Theme;
//        int actionbarId = android.support.v7.appcompat.R.styleable.Theme_windowActionBar;
//
//        TypedArray a = lastTheme.obtainStyledAttributes(themeId);
//        TypedArray b = newTheme.obtainStyledAttributes(themeId);
//
//        try {
//            Method applyStyle = Resources.Theme.class.getMethod("applyStyle", new Class[]{int.class, boolean.class});
//            Method [] methods = TypedArray.class.getDeclaredMethods();
//            Method obtain = TypedArray.class.getDeclaredMethod("obtain", Resources.class, int.class);
//            obtain.setAccessible(true);
//
//            TypedArray array1 = (TypedArray)obtain.invoke(null, lastResources, themeId.length);
//            TypedArray array2 = (TypedArray)obtain.invoke(null, newResources, themeId.length);
//
//            obtain.setAccessible(false);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        boolean aa = a.hasValue(actionbarId);
//        boolean bb = b.hasValue(actionbarId);

        mAssetManager = assetManager;
        mResources = newResources;
        mTheme = newTheme;
    }

    private boolean addAssetPath(AssetManager assetManager){
        if (assetManager == null){
            return false;
        }

        try{
            Method addAssetPath = ReflectionHelper.getMethod(AssetManager.class,
                    "addAssetPath",
                    String.class);
            String inner = PathUtil.getInnerApkPath(pluginName);
            addAssetPath.invoke(assetManager, inner);
        }catch (Exception e){
            XPluginLog.i(e);
            return false;
        }
        return true;
    }

    protected ClassLoader getClassLoader(){
        if (classLoader == null){
            classLoader = createClassLoader();
        }
        return classLoader;
    }

    public AssetManager getAssetManager(){
        if (mAssetManager == null){
            mAssetManager = createAssetManager();
        }
        return mAssetManager;
    }

    public Resources getResources(){
        return mResources;
    }

    public Resources.Theme getTheme(){
        return mTheme;
    }

    public static class Builder{

        private String pluginName;

        public Builder(){}

        public Builder setApkPath(String pluginName){
            this.pluginName = pluginName;
            return this;
        }

        public XPlugin build(){
            if (TextUtils.isEmpty(pluginName)){
                throw new IllegalArgumentException("pluginName不能为空");
            }
            return new XPlugin(this);
        }
    }
}
